package com.hmall.item.interceptor;

import com.hmall.item.context.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Auth：Yang Zhen
 * @Desc：item-service 服务的拦截器
 * @Date：2023/4/20 9:05
 */
@Slf4j
public class AuthorizationInterceptor implements HandlerInterceptor {

    /**
     * 功能描述：拦截所有用户的请求，获取请求头的 authorization，用户的 ID
     *
     * @param request
     * @param response
     * @param handler
     * @return：boolean
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String authorization = request.getHeader("authorization");
        if (authorization == null){
            log.error("feign未携带请求头！！！");
            return false;
        }
        BaseContext.setCurrentId(Long.valueOf(authorization));
        Long currentId = BaseContext.getCurrentId();
        log.info("item-service 层获取到的用户ID：" + currentId);
        return true;
    }

}
